﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections;
using DeviceInfoManage.DataAccessHelper;
using DeviceInfoManage.DataAccessLayer;
/// <summary>
/// LendInfo 的摘要说明
/// </summary>
/// 
namespace DeviceInfoManage.BusinessLogicLayer
{
    //LendInfo类实现设备的外借和归还业务，它对应数据库中的lendInfo表
    /*设备借用信息表
      CREATE TABLE [dbo].[lendInfo] (
	    [lendId] [int] IDENTITY (1, 1) NOT NULL ,			                //借用记录编号
	    [deviceId] [int] NOT NULL ,						                    //设备编号
	    [lendDepartmentId] [int] NOT NULL ,					                //借用设备部门编号
	    [lendPerson] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,	//借用设备人
	    [lendDate] [datetime] NOT NULL ,					                //借用设备日期
	    [lendDays] [smallint] NOT NULL ,					                //借用设备天数
	    [returnDate] [datetime] NULL ,						                //归还设备时间
	    [isReturn] [bit] NOT NULL 						                    //设备是否已经归还
       ) ON [PRIMARY]
     */
    public class LendInfo
    {
        private int lendId;             /*借用编号*/
        private int deviceId;           /*设备编号*/
        private int lendDepartmentId;   /*借用设备部门编号*/
        private string lendPerson;      /*借用设备人*/
        private DateTime lendDate;      /*借用设备日期*/
        private int lendDays;           /*借用设备天数*/
        private DateTime returnDate;    /*归还设备时间*/
        private int isReturn;           /*设备是否已经归还,0-未归还,1-已归还*/
        private string errMessage;      /*保存业务处理错误信息*/

        /*各个字段对外显示的属性*/
        #region MyRegion
        public int LendId
        {
            set { this.lendId = value; }
            get { return this.lendId; }
        }
        public int DeviceId
        {
            set { this.deviceId = value; }
            get { return this.deviceId; }
        }
        public int LendDepartmentId
        {
            set { this.lendDepartmentId = value; }
            get { return this.lendDepartmentId; }
        }
        public string LendPerson
        {
            set { this.lendPerson = value; }
            get { return this.lendPerson; }
        }
        public DateTime LendDate
        {
            set { this.lendDate = value; }
            get { return this.lendDate; }
        }
        public int LendDays
        {
            set { this.lendDays = value; }
            get { return this.lendDays; }
        }
        public DateTime ReturnDate
        {
            set { this.returnDate = value; }
            get { return this.returnDate; }
        }
        public int IsReturn
        {
            set { this.isReturn = value; }
            get { return this.isReturn; }
        }
        public string ErrMessage
        {
            set { this.errMessage = value; }
            get { return this.errMessage; }
        }
        #endregion
        
        /*构造函数进行初始化*/
        public LendInfo()
        {
            lendId = 0;
            deviceId = 0;
            lendDepartmentId = 0;
            lendPerson = "";
            lendDate = DateTime.MinValue;
            lendDays = 0;
            returnDate = DateTime.MaxValue;
            isReturn = 0;
        }

        /*实现设备外借业务逻辑的处理，外借信息预先保存到了各个对应的字段中了*/
        public bool AddLendInfo()
        {
            /*判断是否已经选择了外借的设备*/
            if (this.deviceId == 0)
            {
                this.errMessage = "你没有选择外借的设备";
                return false;
            }
            /*判断该设备在系统中是否已经存在*/
            if (!(new DeviceInfo()).IsExist(deviceId))
            {
                this.errMessage = "不存在该设备编号的设备信息";
                return false;
            }
            /*检查设备状态*/
            if ((new DeviceInfo()).GetDeviceState(deviceId) != 1)
            {
                this.errMessage = "该设备未处于空闲状态，不能外借!";
                return false;
            }
            /*通过验证后，可以执行借出信息的添加了*/
            string insertString = "insert into lendInfo(deviceId,lendDepartmentId,lendPerson,lendDate,lendDays,isReturn) values (";
            insertString += deviceId + ",";
            insertString += lendDepartmentId + ",";
            insertString += SqlString.GetQuotedString(lendPerson) + ",'";
            insertString += lendDate + "',";
            insertString += lendDays + ",";
            insertString += isReturn + ")";
            /*设备外借后要修改该设备的状态为外借状态,系统中外借状态用数字2表示的*/
            string updateString = "update deviceInfo set deviceState=2 where deviceId=" + deviceId;
            string[] sqlStrings = new string[]{insertString,updateString};
            DataBase db = new DataBase();
            /*调用数据层的ExecuteSQL执行批量的sql产生的事务操作，成功返回true*/
            if (db.ExecuteSQL(sqlStrings) == false)
            {
                this.errMessage = "设备外借进行数据库操作时发生了错误!";
                return false;
            }
            return true;
        }

        /*根据条件查询外借的还没有归还的设备信息*/
        public DataSet QueryLendInfo(string deviceName, string deviceType, string startTime, string endTime)
        {
            string queryString = "select * from lendInfoView where deviceName like '%" + deviceName;
            queryString += "%' and deviceTypeName like '%" + deviceType + "%'";
            /*如果用户设置了外借开始时间查询条件*/
            if (startTime != "")
                queryString += " and lendDate > '" + Convert.ToDateTime(startTime) + "'";
            /*如果用户设置了外借结束时间查询条件*/
            if (endTime != "")
                queryString += " and lendDate <'" + Convert.ToDateTime(endTime) + "'";
            /*设置查询还没有归还设备的外借信息*/
            queryString += " and isReturn=0";
            DataBase db = new DataBase();
            return db.GetDataSet(queryString);
        }
   
        /*根据外借编号得到该设备的借用信息*/
        public bool GetLendInfo(int lendId)
        {
            string queryString = "select * from lendInfo where lendId = " + lendId;
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            if (ds.Tables[0].Rows.Count == 0)
            {
                this.ErrMessage = "不存在该设备借用信息记录";
                return false;
            }
            /*如果查找到该记录，下面用数据库中的各个字段填充本类的对应字段*/
            DataRow dr = ds.Tables[0].Rows[0];
            this.lendId = lendId;
            this.deviceId = Int32.Parse(dr["lendId"].ToString());
            this.lendDepartmentId = Int32.Parse(dr["lendDepartmentId"].ToString());
            this.lendPerson = dr["lendPerson"].ToString();
            this.lendDate = Convert.ToDateTime(dr["lendDate"]);
            this.lendDays = Int32.Parse(dr["lendDays"].ToString());
            if(!dr["returnDate"].Equals(System.DBNull.Value))
                this.returnDate = Convert.ToDateTime(dr["returnDate"]);

            this.isReturn = Convert.ToInt16(dr["isReturn"]);
            return true;
        }

        /*设备归还业务逻辑*/
        public bool UpdateLendInfo(int lendId, DateTime returnDate)
        {
            /*修改出借信息的sql语句*/
            string lendInfoUpdateString = "update lendInfo set returnDate = '" + returnDate;
            lendInfoUpdateString += "',isReturn = 1 where lendId = " + lendId;
            /*根据出借编号得到出借的设备编号*/
            int deviceId = this.GetDeviceId(lendId);
            /*修改设备状态的sql语句*/
            string deviceStateUpdateString = "update deviceInfo set deviceState = 1 where deviceId = " + deviceId;
            /*将上面两条更新的sql语句组成一个事务操作*/
            string[] updateStrings = new string[] {lendInfoUpdateString,deviceStateUpdateString};
            /*调用数据层进行数据的批处理更新,成功返回true*/
            DataBase db = new DataBase();
            if (!db.ExecuteSQL(updateStrings))
            {
                this.errMessage = "执行设备归还操作时发生了错误!";
                return false;
            }
            return true;
        }

        /*根据设备外借记录编号得到外借的设备编号*/
        public int GetDeviceId(int lendId)
        {
            string queryString = "select deviceId from lendInfo where lendId = " + lendId;
            DataSet ds = (new DataBase()).GetDataSet(queryString);
            return Int32.Parse(ds.Tables[0].Rows[0]["deviceId"].ToString());
        }

        /*得到所有已经超过借用期限还没有归还的外借信息*/
        public ArrayList GetDeviceArriveTimeInfoList()
        {
            /*此业务逻辑代码被作者屏蔽,需要的请联系作者购买*/
            return null;
        }
    }

}

